﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Jumpton.SFA.Server.BusinessRule.MasterData;
using Jumpton.SFA.Server.Entity;
using Jumpton.SFA.Server.BusinessRule;
using Jumpton.SFA.Server.BusinessRule.System;
using Jumpton.SFA.Server.BusinessRule.InStoreDataMeasures;
using Jumpton.Persistent;
using Jumpton.SFA.Server.BusinessRule.CallRoute;

public partial class CallRoute_Visit_Type_Maitain : PageBase
{

    public string ID;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindVisitType();
            BindConfig();
        }
    }

    private void BindVisitType()
    {
        GridPager1.DataSource = new VisitTypeManager().LoadTypes(MyStateManager.DomainID, null);
        GridPager1.GridView = VisistTypeList;
        GridPager1.BindData();
     
    }
    protected void BindConfig()
    {
        ArrayList conf = new ArrayList();
        string hidvalue=HiddenField1.Value;
        hidvalue=hidvalue.Substring(1,hidvalue.Length-2);
        string[] c = hidvalue.Split('|');
        int list = 0;
        foreach (string s in c)
        {
            conf.Add(new Configs(s,list++)); 
        }

        DataList1.DataSource = conf;  
        DataList1.DataBind();
    }
   

    protected void btnSave_Click(object sender, EventArgs e)
    {
       
        VisitTypeManager vm=new VisitTypeManager();
        VisitType vt = null;
        try
        {
            if (HiddenField2.Value != "")
            {
                try
                {
                    vt = vm.LoadType(int.Parse(HiddenField2.Value));
                }
                catch
                {
                    vt = new VisitType();
                    vt.CREATETIME = BusinessLogic.GetDateTimeDecimal(DateTime.Now);
                }
            }
            else
            {
                vt = new VisitType();
                vt.CREATETIME = BusinessLogic.GetDateTimeDecimal(DateTime.Now);
            }
            string config = HiddenField1.Value;
            foreach (DataListItem dli in DataList1.Items)
            {
                CheckBox c = (CheckBox)dli.FindControl("CheckBox1");
                if (c != null)
                {
                    if (c.Checked)
                    {
                        config = config.Replace("|" + c.Text + "|", "|1|");
                    }
                    else
                        config = config.Replace("|" + c.Text + "|", "|0|");
                }
            }
            config = config.Substring(1, config.Length - 1);
            if (txt_gpstime.Text != "")
                config += txt_gpstime.Text.Trim();
            else
                config += "15";
            vt.DESCRIPTION = txt_description.Text.Trim();
            vt.ACTIVE = chb_Active.Checked;
            vt.CONFIG = config;
            vt.DOMAINID = MyStateManager.DomainID;
            vt.LAST_UPDATER = MyStateManager.Name;
            vt.LAST_UPDATE_TIME = BusinessLogic.GetDateTimeDecimal(DateTime.Now);
            if (vm.Save(vt))
            { 
                //if (HiddenField2.Value != "")
                //{
                //     ShowMessage("保存成功",Request.RawUrl.ToString());
                //}
                //else
                //{
                //    ShowMessage("保存成功");
                //    BindVisitType();
                //}
                ShowMessage("保存成功", Request.RawUrl.ToString());
            }
            else
                ShowMessage("保存失败");

        }
        catch(Exception ex) {
            ShowMessage("保存失败"+ex.Message);
        
        }
        
    }

    class Configs
    {
        public Configs(string name,int list)
        {
            Name = name;
            List = list;
        }
        public String Name
        {
            get;
            set;
        }
        public int List
        {
            get;
            set;
        }
    }
    protected void VisistTypeList_SelectedIndexChanged(object sender, EventArgs e)
    {
        GridViewRow gr=VisistTypeList.SelectedRow;
        LinkButton lbtn_id = (LinkButton) gr.FindControl("LinkButton1");
        if (lbtn_id != null)
        {
            HiddenField2.Value = lbtn_id.Text.Trim();
            VisitType vt= new VisitTypeManager().LoadType(int.Parse(HiddenField2.Value));
            if (vt != null)
            {
                txt_description.Text = vt.DESCRIPTION;
                txt_gpstime.Text = vt.CONFIG.Substring(vt.CONFIG.LastIndexOf("|")+1);
                chb_Active.Checked = vt.ACTIVE;
                string[] config=vt.CONFIG.Substring(0,vt.CONFIG.LastIndexOf("|")).Split('|');
                int i=0;
                foreach (DataListItem dli in DataList1.Items)
                {
                    CheckBox c = (CheckBox)dli.FindControl("CheckBox1");
                    if (c != null)
                    {
                        if (i < config.Length)
                            c.Checked = config[i++]=="1"?true:false;

                    }
                }
            }
        }
    }
    protected void btn_add_Click(object sender, EventArgs e)
    {
        HiddenField2.Value = "";
        txt_gpstime.Text = "15";
        txt_gpstime.Text = "";
        BindConfig();
    }
}
